package com.example.shaoying.yuejian.fragment.mainFragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.shaoying.yuejian.R;
import com.example.shaoying.yuejian.adapter.ActAdapter;
import com.example.shaoying.yuejian.javabean.ListResult;
import com.example.shaoying.yuejian.model.ActivityModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class ActivityFragment extends Fragment {
    private  View view;
    private RecyclerView recyclerView;
    private List<ListResult> list;
    private Context context;
    private ActAdapter actAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view= inflater.inflate(R.layout.huodong, container, false);
        init();
        event();
        return view;
    }
    public void init()
    {
        context=getActivity();
        recyclerView=(RecyclerView)view.findViewById(R.id.L_acvitity);
    }
    public void event()
    {

        ActivityModel model=new ActivityModel();

        Call<List<ListResult>> call= model.getActList();;
        call.enqueue(new Callback<List<ListResult>>() {
            @Override
            public void onResponse(Call<List<ListResult>> call, Response<List<ListResult>> response) {
                list=response.body();
                ActAdapter actAdapter=new ActAdapter(context,list);
                LinearLayoutManager manager=new LinearLayoutManager(context);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(manager);
                recyclerView.setAdapter(actAdapter);
                recyclerView.setItemAnimator(new DefaultItemAnimator());

            }

            @Override
            public void onFailure(Call<List<ListResult>> call, Throwable t) {
                Toast.makeText(context,"列表获取失败",Toast.LENGTH_SHORT).show();
            }
        });
    }

}
